﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LockCrypt.Core;
using LockCrypt.Properties;
using LockCrypt.DAL;
using WPFLocalizeExtension.Engine;

namespace LockCrypt {
    public class WPFConfigurationManager : ICoreConfiguration, IUIConfiguration, IDalConfiguration {
        #region UI Configuration
        public string LastImportPath { get; set; }
        #endregion

        #region Core configuration
        public bool FirstRun { get; set; }
        public string Language { get; set; }
        #endregion

        #region DAL configuration
        /// <summary>
        /// Gets or sets the database provider.
        /// </summary>
        /// <value>The database provider.</value>
        public IDbProvider Provider { get; set; }
        #endregion

        internal static void Configure() {
            Configure(false);
        }

        /// <summary>
        /// Configures the application.
        /// </summary>
        /// <param name="forceReinitializeProvider"><c>true</c> if the database provider should be reinitialized.</param>
        internal static void Configure(bool forceReinitializeProvider) {
            IDbProvider dbProvider = null;
            if(Settings.Default.AutoOpenOnStartup) {
                if(DalConfigurationManager.Provider.GetType() != typeof(NullDbProvider) && !forceReinitializeProvider) { // existing provider which we don't want to reinitialize
                    dbProvider = DalConfigurationManager.Provider;
                } else {
                    EncryptionProviders encryptionAlgorithm;
                    try {
                        encryptionAlgorithm = (EncryptionProviders)Enum.Parse(typeof(EncryptionProviders), Settings.Default.EncryptionAlgorithm, true);
                    } catch {
                        encryptionAlgorithm = EncryptionProviders.None;
                    }
                    Constants.DatabaseType databaseType;
                    try {
                        databaseType = (Constants.DatabaseType)Enum.Parse(typeof(Constants.DatabaseType), Settings.Default.DatabaseMode, true);
                    } catch {
                        databaseType = Constants.DatabaseType.XML;
                    }
                    dbProvider = DalConfigurationManager.GetProvider(databaseType, encryptionAlgorithm, null, Settings.Default.DatabaseConnectionString);
                    /*if(DalConfigurationManager.IsProviderFileBased(databaseType)) {
                      dbProvider = DalConfigurationManager.GetProvider(databaseType, encryptionAlgorithm, null, Settings.Default.DatabaseConnectionString);  
                    } else {
                        dbProvider = DalConfigurationManager.GetProvider(databaseType, encryptionAlgorithm, null, Settings.Default.DatabasePath);
                    }*/
                }
            } else {
                dbProvider = new NullDbProvider();
            }
            WPFConfigurationManager config = new WPFConfigurationManager() {
                Provider = dbProvider,
                FirstRun = Settings.Default.FirstRun,
                Language = Settings.Default.Language
            };

            UIConfigurationManager.Configure(config);
            CoreConfigurationManager.Configure(config);
            DalConfigurationManager.Configure(config);
            LocalizeDictionary.Instance.Culture = new System.Globalization.CultureInfo(CoreConfigurationManager.Language);
        }
    }

}
